package com.jh.user.pojo.freeQuick;

import cn.jh.common.pay.client.FreeQuickRequest;
import cn.jh.common.pay.enumtype.EncryptTypeEnum;
import cn.jh.common.utils.ChannelType;
import cn.jh.common.utils.CommonConstants;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jh.user.pojo.ChannelRate;
import com.jh.user.pojo.User;
import com.jh.user.pojo.UserBankInfo;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 通道商户注册表
 */

@Entity
@Table(name = "t_passage_way_register")
public class PassageWayRegister implements Serializable {


    private static final long serialVersionUID = 3568676856260346869L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    /**
     * 用户id
     */
    @Column(name="user_id")
    private String userId;

    /**
     * 通道银行卡绑定状态,0未绑卡，1：待确定，2：绑定成功，3：绑定失败
     */
    @Column(name="bank_status")
    private String bankStatus;

    /**
     * 费率或费
     */
    @Transient
    private BigDecimal feeValue;

    /**
     * 通道商户编号
     */

    @Column(name="merchant_no")
    private String merchantNo;

    /**
     * 商户请求号
     */

    @Column(name="request_no")
    private String requestNo;

    /**
     * 商户名称
     */
    @Column(name="merchant_name")
    private String merchantName;

    /**
     * 商户简称
     */
    @Column(name="short_name")
    private String shortName;

    /**
     * 产品类型
     */
    @Column(name="channel_product_code")
    private String channelProductCode;

    /**
     * 绑定手机号
     */
    @Column(name="bind_mobile")
    private String bindMobile;

    /**
     * 邮箱
     */
    @Column(name="bind_email")
    private String bindEmail;

    /**
     * 居住地址
     */
    @Column(name="address")
    private String address;

    /**
     * 证件号码
     */
    @Column(name="id_card_no")
    private String idCardNo;

    /**
     * 结算银行卡号
     */
    @Column(name="settle_bank_account_no")
    private String settleBankAccountNo;

    /**
     * 结算卡银行开户名
     */
    @Column(name="settle_bank_account_name")
    private String settleBankAccountName;

    /**
     * 银行卡类型
     */
    @Column(name="settle_bank_account_type")
    private String settleBankAccountType;

    /**
     * 结算卡银行名称
     */
    @Column(name="settle_bank_name")
    private String settleBankName;

    /**
     * 结算卡开户支行名称
     */
    @Column(name="settle_bank_sub_name")
    private String settleBankSubName;

    /**
     * 结算卡银行缩写
     */
    @Column(name="settle_bank_abbr")
    private String settleBankAbbr;

    /**
     * 结算卡开户行联行号
     */
    @Column(name="settle_bank_channel_no")
    private String settleBankChannelNo;

    /**
     * 结算卡开户省
     */
    @Column(name="settle_bank_card_province")
    private String settleBankCardProvince;

    /**
     * 结算卡开户市
     */
    @Column(name="settle_bank_card_city")
    private String settleBankCardCity;

    /**
     * 结算类型
     */
    @Column(name="settlement_type")
    private String settlementType;

    /**
     * 借记卡费率
     */
    @Column(name="debit_rate")
    private BigDecimal debitRate;

    /**
     * 借记卡封顶金额
     */
    @Column(name="debit_cap_amount")
    private String debitCapAmount;

    /**
     * 贷记卡费率
     */
    @Column(name="credit_rate")
    private BigDecimal creditRate;

    /**
     * 贷记卡封顶金额
     */
    @Column(name="credit_cap_amount")
    private String creditCapAmount;

    /**
     * 提现费率
     */
    @Column(name="withdraw_deposit_rate")
    private BigDecimal withdrawDepositRate;

    /**
     * 单笔提现手续费
     */
    @Column(name="withdraw_deposit_single_fee")
    private BigDecimal withdrawDepositSingleFee;

    /**
     * 通道注册状态，1：成功，2：失败
     */
    @Column(name="register_state")
    private String registerState;

    /**
     * 密钥，用于请求接口
     */
    @Column(name="encrypt_key")
    private String encryptKey;

    /**
     * 代还费率
     */
    @Column(name="withd_replace_fee")
    private BigDecimal withdReplaceFee;

    /**
     * 创建时间
     */
    @Column(name="create_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private String createTime;

    /**
     * 创建人
     */
    @Column(name="create_by")
    private String createBy;

    /**
     * 更新时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name="update_time")
    private String updateTime;

    /**
     * 更新人
     */
    @Column(name="update_by")
    private String updateBy;

    /**
     * 银行代码
     */
    @Column(name = "abb_reviation")
    private String abbReviation;

    /**
     * 上游通道返回商户号
     */
    @Transient
    private String channelMerchantNo;

    /**
     * 后台可设置该通道注册信息是否可用，不可用不能修改费率等信息：0可用，1不可用
     */
    @Column(name = "pwr_status")
    private String pwrStatus;

    public String getChannelMerchantNo() {
        return channelMerchantNo;
    }

    public void setChannelMerchantNo(String channelMerchantNo) {
        this.channelMerchantNo = channelMerchantNo;
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }

    public String getRequestNo() {
        return requestNo;
    }

    public void setRequestNo(String requestNo) {
        this.requestNo = requestNo;
    }

    public String getMerchantName() {
        return merchantName;
    }

    public void setMerchantName(String merchantName) {
        this.merchantName = merchantName;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getChannelProductCode() {
        return channelProductCode;
    }

    public void setChannelProductCode(String channelProductCode) {
        this.channelProductCode = channelProductCode;
    }

    public String getBindMobile() {
        return bindMobile;
    }

    public void setBindMobile(String bindMobile) {
        this.bindMobile = bindMobile;
    }

    public String getBindEmail() {
        return bindEmail;
    }

    public void setBindEmail(String bindEmail) {
        this.bindEmail = bindEmail;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getIdCardNo() {
        return idCardNo;
    }

    public void setIdCardNo(String idCardNo) {
        this.idCardNo = idCardNo;
    }

    public String getSettleBankAccountNo() {
        return settleBankAccountNo;
    }

    public void setSettleBankAccountNo(String settleBankAccountNo) {
        this.settleBankAccountNo = settleBankAccountNo;
    }

    public String getSettleBankAccountName() {
        return settleBankAccountName;
    }

    public void setSettleBankAccountName(String settleBankAccountName) {
        this.settleBankAccountName = settleBankAccountName;
    }

    public String getSettleBankAccountType() {
        return settleBankAccountType;
    }

    public void setSettleBankAccountType(String settleBankAccountType) {
        this.settleBankAccountType = settleBankAccountType;
    }

    public String getSettleBankName() {
        return settleBankName;
    }

    public void setSettleBankName(String settleBankName) {
        this.settleBankName = settleBankName;
    }

    public String getSettleBankSubName() {
        return settleBankSubName;
    }

    public void setSettleBankSubName(String settleBankSubName) {
        this.settleBankSubName = settleBankSubName;
    }

    public String getSettleBankAbbr() {
        return settleBankAbbr;
    }

    public void setSettleBankAbbr(String settleBankAbbr) {
        this.settleBankAbbr = settleBankAbbr;
    }

    public String getSettleBankChannelNo() {
        return settleBankChannelNo;
    }

    public void setSettleBankChannelNo(String settleBankChannelNo) {
        this.settleBankChannelNo = settleBankChannelNo;
    }

    public String getSettleBankCardProvince() {
        return settleBankCardProvince;
    }

    public void setSettleBankCardProvince(String settleBankCardProvince) {
        this.settleBankCardProvince = settleBankCardProvince;
    }

    public String getSettleBankCardCity() {
        return settleBankCardCity;
    }

    public void setSettleBankCardCity(String settleBankCardCity) {
        this.settleBankCardCity = settleBankCardCity;
    }

    public String getSettlementType() {
        return settlementType;
    }

    public void setSettlementType(String settlementType) {
        this.settlementType = settlementType;
    }

    public String getDebitCapAmount() {
        return debitCapAmount;
    }

    public void setDebitCapAmount(String debitCapAmount) {
        this.debitCapAmount = debitCapAmount;
    }

    public String getCreditCapAmount() {
        return creditCapAmount;
    }

    public void setCreditCapAmount(String creditCapAmount) {
        this.creditCapAmount = creditCapAmount;
    }

    public String getRegisterState() {
        return registerState;
    }

    public void setRegisterState(String registerState) {
        this.registerState = registerState;
    }

    public String getEncryptKey() {
        return encryptKey;
    }

    public void setEncryptKey(String encryptKey) {
        this.encryptKey = encryptKey;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public BigDecimal getFeeValue() {
        return feeValue;
    }

    public void setFeeValue(BigDecimal feeValue) {
        this.feeValue = feeValue;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getDebitRate() {
        return debitRate;
    }

    public void setDebitRate(BigDecimal debitRate) {
        this.debitRate = debitRate;
    }

    public BigDecimal getCreditRate() {
        return creditRate;
    }

    public void setCreditRate(BigDecimal creditRate) {
        this.creditRate = creditRate;
    }

    public BigDecimal getWithdrawDepositRate() {
        return withdrawDepositRate;
    }

    public void setWithdrawDepositRate(BigDecimal withdrawDepositRate) {
        this.withdrawDepositRate = withdrawDepositRate;
    }

    public BigDecimal getWithdrawDepositSingleFee() {
        return withdrawDepositSingleFee;
    }

    public void setWithdrawDepositSingleFee(BigDecimal withdrawDepositSingleFee) {
        this.withdrawDepositSingleFee = withdrawDepositSingleFee;
    }

    public BigDecimal getWithdReplaceFee() {
        return withdReplaceFee;
    }

    public void setWithdReplaceFee(BigDecimal withdReplaceFee) {
        this.withdReplaceFee = withdReplaceFee;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getBankStatus() {
        return bankStatus;
    }

    public void setBankStatus(String bankStatus) {
        this.bankStatus = bankStatus;
    }

    public String getAbbReviation() {
        return abbReviation;
    }

    public void setAbbReviation(String abbReviation) {
        this.abbReviation = abbReviation;
    }

    public String getPwrStatus() {
        return pwrStatus;
    }

    public void setPwrStatus(String pwrStatus) {
        this.pwrStatus = pwrStatus;
    }

    //银行卡和费率
    public void addMap(UserBankInfo bankInfo) {
        setSettleBankAccountNo(bankInfo.getCardNo());
        setSettleBankAccountType(bankInfo.getCardType());
        setBindMobile(bankInfo.getPhone());
        setSettleBankCardCity(bankInfo.getCity());
        setSettleBankCardProvince(bankInfo.getProvince());
        setSettleBankChannelNo(bankInfo.getLineNo());
        setSettleBankAbbr(bankInfo.getBankAbbr());
        setSettleBankSubName(bankInfo.getBankBranchName());
        setSettleBankName(bankInfo.getBankName());
        setIdCardNo(bankInfo.getIdcard());
        setAbbReviation(bankInfo.getAbbreviation());
    }

    //注册
    public FreeQuickRequest transform(User user, UserBankInfo card, ChannelRate channelRate ) {
        String rate = String.valueOf(channelRate.getRate());
        FreeQuickRequest freeQuickRequest = new FreeQuickRequest(CommonConstants.AGENT_NO, CommonConstants.AGENT_KEY);
        freeQuickRequest.setSupportSign(false);
        freeQuickRequest.setEncryptType(EncryptTypeEnum.AES);
        setBindEmail(user.getEmail());
        setAddress(user.getAddress());
        setBindMobile(card.getPhone());
        setSettleBankAccountNo(card.getCardNo());
        setSettleBankAccountName(card.getUserName());
        setSettleBankName(card.getBankName());
        setSettleBankSubName(card.getBankBranchName());
        setSettleBankAbbr(card.getAbbreviation());
        setSettleBankChannelNo(card.getLineNo());
        setSettleBankCardProvince(card.getProvince());
        setSettleBankCardCity(card.getCity());

        setIdCardNo(card.getIdcard());
        setMerchantName(card.getUserName());
        setShortName(card.getUserName());
        setAbbReviation(card.getBankAbbr());
        setSettleBankAccountType(CommonConstants.PRIVATE);
        setSettlementType(CommonConstants.LEMENT_TYPE);
        setDebitRate(channelRate.getRate());
        setCreditRate(channelRate.getRate());
        setWithdrawDepositSingleFee(channelRate.getRate());
        String channelProductCode = ChannelType.of(Integer.valueOf(getChannelProductCode())).getValue();

        freeQuickRequest.addParam("bindEmail", user.getEmail());//邮箱
        freeQuickRequest.addParam("address", card.getProvince()+card.getCity());//居住地址
        freeQuickRequest.addParam("idCardNo", card.getIdcard());//身份证号
        freeQuickRequest.addParam("merchantName",card.getUserName());//商户名称
        freeQuickRequest.addParam("shortName", card.getUserName());//商户简称
        freeQuickRequest.addParam("bindMobile", card.getPhone());//绑定手机号
        freeQuickRequest.addParam("settleBankAccountNo", card.getCardNo());//结算银行卡号
        freeQuickRequest.addParam("settleBankAccountName", card.getUserName());//结算卡银行开户名
        freeQuickRequest.addParam("settleBankName", card.getBankName());//银行名称
        freeQuickRequest.addParam("settleBankSubName", card.getBankBranchName());//结算卡开户支行名称
        freeQuickRequest.addParam("settleBankAbbr", card.getAbbreviation());// 结算卡银行缩写
        freeQuickRequest.addParam("settleBankChannelNo", card.getLineNo());//结算卡开户行联行号
        freeQuickRequest.addParam("settleBankCardProvince", card.getProvince());//结算卡开户省
        freeQuickRequest.addParam("settleBankCardCity", card.getCity());//结算卡开户市
        freeQuickRequest.addParam("settleBankId", "");//银行Id,HFTXPAY，YKTPAY通道必传
        freeQuickRequest.addParam("debitRate", rate);//借记卡费率
        freeQuickRequest.addParam("debitCapAmount", "");//借记卡封顶金额
        freeQuickRequest.addParam("creditRate", rate);//贷记卡费率
        freeQuickRequest.addParam("creditCapAmount", "");//贷记卡封顶金额
        freeQuickRequest.addParam("withdrawDepositRate",  "0");//提现费率
        freeQuickRequest.addParam("withdrawDepositSingleFee", String.valueOf(channelRate.getExtraFee()));//单笔提现手续费
        freeQuickRequest.addParam("requestNo", getRequestNo());//商户请求号
        freeQuickRequest.addParam("merchantNo", getMerchantNo());//商户请求号
        freeQuickRequest.addParam("channelProductCode", channelProductCode); //产品类型
        freeQuickRequest.addParam("settleBankAccountType", CommonConstants.PRIVATE);//银行卡类型
        freeQuickRequest.addParam("settlementType", CommonConstants.LEMENT_TYPE);//结算类型
        freeQuickRequest.addParam("idCardPhoto","");
        freeQuickRequest.addParam("idCardBackPhoto","");
        freeQuickRequest.addParam("bankCardPhoto","");
        freeQuickRequest.addParam("personPhoto","");
        return freeQuickRequest;
    }


}
